package com.a01_2021.thousandstimes.instance.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import com.a01_2021.thousandstimes.general.service.SysUserService;
import com.a01_2021.thousandstimes.instance.entity.OffWork;
import com.a01_2021.thousandstimes.instance.mapper.OffWorkMapper;
import com.a01_2021.thousandstimes.instance.service.IOffWorkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author E1xP
 * @since 2021-03-21
 */
@Service
public class OffWorkServiceImpl extends ServiceImpl<OffWorkMapper, OffWork> implements IOffWorkService {

    @Resource
    OffWorkMapper offWorkMapper;
    @Resource
    SysUserService userService;

    /**
     * 添加请假申请
     *
     * @param userId      用户id
     * @param offWorkDate 请假日期
     * @param type        请假类型
     * @param reason      请假理由
     * @return 请假实体
     * @throws CustomException 当用户不存在时抛出该异常
     */
    @Override
    public OffWork request(long userId, LocalDate offWorkDate, int type, String reason) throws CustomException {
        if (!userService.isUser(userId)) {
            throw new CustomException(CustomExceptionType.USER_NOT_FOUND_ERROR, "找不到该用户");
        }
        if (offWorkDate == null) {
            //请假日期为空
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "请假时间不得为空");
        }
        if (this.hasRequest(userId, offWorkDate)) {
            //若已有请假存在
            throw new CustomException(CustomExceptionType.OFFWORK_HAD_BEEN_REQUEST_ERROR, "已有请假存在");
        }
        OffWork offWork = new OffWork(null, userId, LocalDateTime.now(), offWorkDate, type, reason, 0, null);
        offWorkMapper.insert(offWork);
        return offWork;
    }

    /**
     * 查询用户是否已在当天请假
     *
     * @param userId      用户id
     * @param offWorkDate 请假日期
     * @return 用户是否已在当天请假
     */
    public boolean hasRequest(long userId, LocalDate offWorkDate) {
        QueryWrapper<OffWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("off_work_date", offWorkDate);
        queryWrapper.eq(false, "stats", "false");
        int count = offWorkMapper.selectCount(queryWrapper);
        return count != 0;
    }

    /**
     * 获取用户的请假请求
     *
     * @param userId 用户id
     * @return 请假记录列表
     * @throws CustomException 当用户不存在时抛出该异常
     */
    @Override
    public List<OffWork> getRequests(long userId) throws CustomException {
        if (!userService.isUser(userId)) {
            throw new CustomException(CustomExceptionType.USER_NOT_FOUND_ERROR);
        }
        QueryWrapper<OffWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return offWorkMapper.selectList(queryWrapper);
    }

    /**
     * 获取全部请假记录
     *
     * @return 请假记录列表
     */
    @Override
    public List<OffWork> getAllRequests() {
        QueryWrapper<OffWork> queryWrapper = new QueryWrapper<>();
        return offWorkMapper.selectList(queryWrapper);
    }

    /**
     * 获取全部未处理请假请求
     *
     * @return 未处理请假请求
     */
    @Override
    public List<OffWork> getALLUnHandleRequests() {
        QueryWrapper<OffWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stats", "unHandle");
        return offWorkMapper.selectList(queryWrapper);
    }

    /**
     * 处理请假请求
     *
     * @param handlerId 处理人id
     * @param requestId 请求id
     * @param stats     处理状态
     * @return OffWork实体
     * @throws CustomException 当请假请求不存在时抛出
     */
    @Override
    public OffWork handleRequest(long handlerId, long requestId, int stats) throws CustomException {
        OffWork offWork = offWorkMapper.selectById(requestId);
        if (offWork == null) {
            //请假不存在
            throw new CustomException(CustomExceptionType.OFFWORK_NOT_FOUNT_ERROR, "找不到该请假记录");
        }
        offWork.setHandler(handlerId);
        offWork.setStats(stats);
        offWorkMapper.updateById(offWork);
        return offWork;
    }


}
